/*
 * IPDXMLConfigFileParameters.java
 *
 * Created on 29 de Março de 2010, 18:26
 *
 */

package intopo.game.impl.ipd;

import intopo.game.XMLConfigFileParameters;

import util.xml.*;

import cern.jet.random.engine.RandomEngine;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.HashMap;

/**
 *
 * @author <a href="mailto:plsm@ua.pt">Pedro Lopes da Silva Mariano</a>
 *
 * @version 1.0 29 de Março de 2010
 */
final public class IPDXMLConfigFileParameters
	implements XMLConfigFileParameters<IteratedPrisonerDilemmaStrategy, IteratedPrisonerDilemmaGame>
{
	final private static String ROOT_NODE = "IteratedPrisonerDilemmaGame";

	final private static Map<String, Constructor<? extends IteratedPrisonerDilemmaStrategy>> STRATEGY_CONSTRUCTORS;
	
	final private static Map<String, Constructor<? extends IteratedPrisonerDilemmaGame>> GAME_CONSTRUCTORS;

	static
	{
		Constructor<? extends IteratedPrisonerDilemmaStrategy> cp;
		STRATEGY_CONSTRUCTORS = new HashMap<String, Constructor<? extends IteratedPrisonerDilemmaStrategy>> ();
		try {
			Class<IteratedPrisonerDilemmaProbabilisticStrategy> c = IteratedPrisonerDilemmaProbabilisticStrategy.class;
			cp = c.getConstructor (Element.class, RandomEngine.class);
			STRATEGY_CONSTRUCTORS.put ("IteratedPrisonerDilemmaProbabilisticPlayer", cp);
		}
		catch (NoSuchMethodException nsme) {
			System.err.println ("Class IteratedPrisonerDilemmaProbabilisticPlayer does not have a constructor with parameter type org.w3c.dom.Element");
			System.exit (1);
		}
		Constructor<IteratedPrisonerDilemmaGame> cg;
		GAME_CONSTRUCTORS = new HashMap<String, Constructor<? extends IteratedPrisonerDilemmaGame>> ();
		try {
			Class<IteratedPrisonerDilemmaGame> c = IteratedPrisonerDilemmaGame.class;
			cg = c.getConstructor (Element.class);
			GAME_CONSTRUCTORS.put ("IteratedPrisonerDilemmaGame", cg);
		}
		catch (NoSuchMethodException nsme) {
			System.err.println ("Class IteratedPrisonerDilemmaGame does not have a constructor with parameter type org.w3c.dom.Element");
			System.exit (1);
		}
	}

	/**
	 * Creates a new instance of IPDXMLConfigFileParameters
	 */
	public IPDXMLConfigFileParameters ()
{
	}

	public String getRootNodeName ()
	{
		return ROOT_NODE;
	}

	/**
	 * Return a map that associates tag names to game constructors. The XML file should only have a single tag whose name is a key of the returned map.
	 */
	public Map<String, Constructor<? extends IteratedPrisonerDilemmaGame>> getGameConstructor ()
	{
		return GAME_CONSTRUCTORS;
	}

	/**
	 * Return a map that associates tag names to strategy constructors.
	 */
	public Map<String, Constructor<? extends IteratedPrisonerDilemmaStrategy>> getStrategyConstructors ()
	{
		return STRATEGY_CONSTRUCTORS;
	}

	public void parse (Node rootNode)
		throws XMLParseException
	{
		AttributeParser<Integer> api;

		api = new AttributeParser<Integer>
			(new CheckExistenceAttribute<Integer> (),
			 new ParseAsInt (0, Integer.MAX_VALUE));

		Node e1, e2;
		if ((e1 = XML.getChildNodeWithName (rootNode, "IteratedPrisonerDilemmaProbabilisticPlayerParameters")) != null) {
			IteratedPrisonerDilemmaProbabilisticStrategy.Parameters.probabilityBin = api.parse ((Element) e1, "probabilityBins");
		}
	}
}
